<?php
/**
 * Create by 11li
 * Date 2020/2/3
 * Time 11:24
 *
 */

namespace app\api\controller\v1;


use app\common\controller\Api;
use app\common\service\LikeService;
use app\common\service\VideoService;
use app\common\validate\Danmu;
use think\Exception;

class Video extends Api
{
	protected $noNeedLogin = ['index'];
	
	public function index() {
		$page = $this->request->get('page', 1, 'intval');
		$goodsId = $this->request->get('goods_id', '', 'intval');
		$res = VideoService::getList($page, $goodsId);
		$this->success('查询成功', $res);
	}
	
	public function like()
	{
		$videoId = $this->request->get('id', '', 'intval');
		!$videoId && $this->error('参数错误');
		
		$obj = new VideoService();
		try {
			$res = $obj->like($videoId, $this->auth->id);
			$this->success('操作成功', $res);
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
	
	public function detail($id) {
		$obj = new VideoService();
		try {
			$res = $obj->getVideoById($id);
			
			//添加浏览记录
			(new LikeService())->addViewRecord($this->auth->id, $res['id']);
			
			$this->success('查询成功', $res);
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
	
	/**
	 * 添加弹幕
	 */
	public function addDanmu()
	{
		$check = new Danmu();
		$data = $this->request->post();
		if(!$check->check($data)) {
			$this->error($check->getError());
		}
		
		$data['user_id'] = $this->auth->id;
		try {
			$res = (new VideoService())->addDanmu($data);
			$this->success('添加成功');
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
	
	/**
	 * 添加视频
	 */
	public function createVideo() {
		$check = new \app\common\validate\Video();
		$data = $this->request->post();
		if(!$check->check($data)) {
			$this->error($check->getError());
		}
		
		
		$data['user_id'] = $this->auth->id;
		$obj = new VideoService();
		try {
			$res = $obj->createVideo($data);
			$this->success('添加成功', ['id' => $res]);
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
	
}